package com.woniu.atms.service.impl;


import com.woniu.atms.entity.G5Car;
import com.woniu.atms.entity.G5CarSeat;
import com.woniu.atms.entity.G5CarType;
import com.woniu.atms.exception.aft.car.CarIsNullException;
import com.woniu.atms.exception.aft.car.CarSeatIsAddException;
import com.woniu.atms.mapper.CarDao;
import com.woniu.atms.mapper.CarSeatDao;
import com.woniu.atms.mapper.CarTypeMapper;
import com.woniu.atms.service.CarSeatService;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (G5CarSeat)表服务实现类
 *
 * @author makejava
 * @since 2022-01-18 10:46:06
 */
@Service("g5CarSeatService")
public class CarSeatServiceImpl implements CarSeatService {
    @Resource
    private CarSeatDao g5CarSeatDao;

    @Resource
    private CarDao carDao;
    @Resource
    private CarTypeMapper carTypeMapper;

    @Override
    public boolean add(G5CarSeat seat) {
        return g5CarSeatDao.insert(seat);
    }

    @Override
    public int addList(List<G5CarSeat> entities) {
        return g5CarSeatDao.insertBatch(entities);
    }

    @Transactional
    @Override
    public boolean addListByCarId(Integer carId) {
        System.out.println(carId);
        // 根据车辆id查询出车辆类型
        G5Car car = carDao.queryById(carId);
        if (car == null) {
            throw new CarIsNullException("没有查询到车辆，无法添加");
        }
        //查询车辆是否已经添加座位
        List<G5CarSeat> list = this.seatList(carId);
        if(list.size()!=0){
            throw new CarSeatIsAddException("车辆已经添加座位，无法再次添加");
        }

        // 根据车辆类型里的座位数量添加座位信息
        G5CarType carType= carTypeMapper.findCarTypeById(car.getTypeId());
        //创建具体车辆的座位列表
        List<G5CarSeat> seats = new ArrayList<>();
        for (Integer i = 1; i <= carType.getSeatNum(); i++) {
            G5CarSeat carSeat = new G5CarSeat("n",carId,i,"y","y");
            //当编号能被4整除或余0时代表靠窗
            if(i%4==1||i%4==0){
                carSeat.setIsWindow("y");
            }
            seats.add(carSeat);
        }
        //批量添加
        int row=g5CarSeatDao.insertBatch(seats);
        if(row!=0){
            return true;
        }
        return false;
    }

    @Override
    public G5CarSeat queryById(Integer id) {
        return g5CarSeatDao.queryById(id);
    }

    @Override
    public List<G5CarSeat> seatList(Integer carId) {
        return g5CarSeatDao.queryListByCarId(carId);
    }

    @Override
    public boolean update(G5CarSeat seat) {
        return g5CarSeatDao.update(seat);
    }

    @Override
    public List<Integer> queryUseSeatIdByDateAndSId(Integer scheduleId, LocalDate date) {
        return g5CarSeatDao.queryUseSeatIdByDateAndSId(scheduleId,date);
    }
}
